/*
 * LCExceptionHandler.java
 *
 * Created on January 9, 2007, 5:27 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wow.play.cricket.common.exception;

import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import wow.play.cricket.common.LCConstants;
import wow.play.cricket.common.exception.LCException;
import wow.play.cricket.common.LCLogger;

/**
 * An application exception handler which will hook into Struts framework and will
 * trap all kind of System (SQLException) / Application (LCException) exceptions.
 * Also it will redirect the unhandled error to error jsp page.
 * @author Jatan Porecha
 * @version $Revision: 2 $
 * Last changed by $Author: nurulsiddik $ on $Date: 2007-03-27 09:50:51 +0000 (Tue, 27 Mar 2007) $ as $Revision: 2 $
 */
public class LCExceptionHandler extends ExceptionHandler
{
    
    private ActionForward errorForward;
    /**
     * Creates a new instance of LCExceptionHandler
     */
    public LCExceptionHandler()
    {
        errorForward = null;
    }
    
    /**
     * This is an overriding method of ExceptionHandler parent class. This will be called
     * by Struts framework in case of any Exception generated by ActionClass.
     * @param ex Exception generated by Action class.
     * @param ae Exception handler configuration details as specified in struts-config.xml
     * @param mapping An ActionMapping object which gives action mapping information for current do
     * request.
     * @param formInstance Action form object for current do request.
     * @param request Http request object.
     * @param response Http response object.
     * @return ActionForward object which would identify the JSP page where the flow should be
     * directed to. It can be calling JSP in case of Application/SQL errors or error jsp
     * in case of unhandled system errors.
     * @throws javax.servlet.ServletException In case if anything goes wrong !!
     */
    public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException
    {
        ActionForward forward=null;
        System.out.println("In the exception handler: "+ex.getMessage() + ex.getClass());
        // since there will be only one exception handler, we can assume that
        // error forward would remain same for all the calls to execute method.
        if(errorForward != null)
            errorForward = new ActionForward(ae.getPath());
        
        if(ex instanceof LCException)
        {
            
            LCException lcEx = (LCException)ex;
            forward = mapping.findForward("error");
            try
            {
                LCLogger.error(lcEx.getErrorMessage());
                request.setAttribute(LCConstants.ERROR_BUNDLE,new Object[]{new String[]{lcEx.getErrorMessage()},lcEx.getErrorMessageCode(),lcEx.getErrorMessageType()});
            }
            catch(Exception e)
            {e.printStackTrace();}
            //lcEx.get
            
            //lcEx.getError_codes().get(0).getError_code()
            
            
        }
        else if(ex instanceof SQLException)
        {
            
            
        }
        else
        {
            ex.printStackTrace();
            throw new ServletException(ex);
        }
        
        return forward;
        
    }
    
    
}
